package com.example.othree.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.PollutionJJJCities;
import com.example.othree.entity.PollutionJJJCities;
import com.example.othree.entity.PollutionJJJCities;
import com.example.othree.service.PollutionJJJCitiesService;
import com.example.othree.service.PollutionJJJCitiesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api")
public class PollutionJJJCitiesController {
    @Autowired
    private PollutionJJJCitiesService pollutionJJJCitiesService;

    @PostMapping("/CitiesJjj/create")
    @ApiOperation("新增")
    public R create(@RequestBody PollutionJJJCities pollutionJJJCities){
        return pollutionJJJCitiesService.save(pollutionJJJCities) ? R.ok() : R.error();
    }

    @PostMapping("/CitiesJjj/saveBatch")
    @ApiOperation("批量新增")
    public R saveBatch(@RequestBody List<PollutionJJJCities> pollutionJJJCitiesList){
        return pollutionJJJCitiesService.saveBatch(pollutionJJJCitiesList) ? R.ok() : R.error();
    }

    @GetMapping("/CitiesJjj/list")
    public List<PollutionJJJCities> list(){
        return pollutionJJJCitiesService.list();
    }

    @PostMapping("/CitiesJjj/update")
    @ApiOperation("修改")
    public R update(@RequestBody PollutionJJJCities pollutionJJJCities){
        return pollutionJJJCitiesService.updateById(pollutionJJJCities) ? R.ok() : R.error();
    }

    @PostMapping("/CitiesJjj/delete/{id}")
    public R delete(@PathVariable Integer id){
        return pollutionJJJCitiesService.removeById(id) ? R.ok() : R.error();
    }

    @PostMapping("/CitiesJjj/listPage")
    @ApiOperation("查询、分页")
    public R listPage(@RequestBody PageParam pageParam){
        HashMap param = pageParam.getParam();
        String hour = param.get("hour").toString();
        String city = param.get("city").toString();
        String date = param.get("date").toString();

        Page<PollutionJJJCities> page = new Page();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<PollutionJJJCities> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(hour) && !"null".equals(hour)){
            lambdaQueryWrapper.eq(PollutionJJJCities::getHour, hour);
        }
        if(StringUtils.isNotBlank(city) && !"null".equals(city)){
            lambdaQueryWrapper.eq(PollutionJJJCities::getCity, city);
        }
        if(StringUtils.isNotBlank(date) && !"null".equals(date)){
            lambdaQueryWrapper.eq(PollutionJJJCities::getDate, date);
        }

        IPage result = pollutionJJJCitiesService.PageList(page, lambdaQueryWrapper);

        return R.ok().data("result", result);
    }
}
